package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.tradewar.TradeWarBuilding;
import com.yanqu.road.utils.property.PropertyHelper;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TradeWarDaoImpl extends TempDao {

	public Map<Integer, Map<Integer, TradeWarBuilding>> getTradeWarBuildingMap() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_trade_war_building";
		Map<Integer, Map<Integer, TradeWarBuilding>> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				TradeWarBuilding data = new TradeWarBuilding();
				data.setType(rs.getInt("type"));
				data.setLevel(rs.getInt("level"));
				data.setOutputSpeedParam(rs.getInt("currencySpeedParam"));
				data.setGetRewardMaxTime(rs.getInt("collectLimitTime"));
				data.setUpgradeConsume(PropertyHelper.parseStringToProperty(rs.getString("consume")));
				data.setUpgradeLimit(rs.getInt("levelUpLimit"));
				if(!dataMap.containsKey(data.getType())){
					dataMap.put(data.getType(), new ConcurrentHashMap<>());
				}
				dataMap.get(data.getType()).put(data.getLevel(), data);
			}

		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}
}
